package com.jkhan.demo.singleton;

/**
 * @ClassName LazySingleton
 * @Description LazySingleton
 * @Author hjk
 * @Date 2022/1/9 19:48
 **/
public class ThreadSafeSingleton {
}

class LazyPerson {
    private LazyPerson() {
    }

    private static LazyPerson instance = null;

    public static synchronized LazyPerson getInstance() {
        if (instance == null) {
            instance = new LazyPerson();
        }
        return instance;
    }
}

class LazyMethod {
    private LazyMethod() {
    }

    private static LazyMethod instance = null;

    public static LazyMethod getInstance() {
        //方式一：效率较差
//        synchronized (LazyMethod.class) {
//            if (instance == null) {
//                instance = new LazyMethod();
//            }
//            return instance;
//        }
        //方式二：效率更高
        if (instance == null) {
            synchronized (LazyMethod.class) {
                if (instance == null) {
                    instance = new LazyMethod();
                }
            }
        }
        return instance;
    }
}